package com.powernode.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.reggie.common.R;
import com.powernode.reggie.entity.Category;
import com.powernode.reggie.entity.Employee;
import com.powernode.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 分类管理
 * @author: www
 * @time: 2023/3/8 16:06
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增成功");
    }

    /**
     * 分类的分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        Page pageInfo = new Page(page,pageSize);//分页构造器对象

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();//条件构造器对象
        queryWrapper.orderByAsc(Category::getSort);//排序条件

        categoryService.page(pageInfo,queryWrapper);//进行分页查询

        return R.success(pageInfo);
    }

    /**
     * 根据id删除分类
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){


        //categoryService.removeById(ids);
        categoryService.remove(ids);//自定义的remove方法，可以判断是否关联菜品
        return R.success("删除成功");
    }

    /**
     * 根据id修改分类方法
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改分类：{}",category);
        categoryService.updateById(category);
        return R.success("修改分类成功");
    }


    /**
     * 根据条件（分类type）查询分类数据 分类数据用来在菜品新增中展示选择
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();//条件构造器
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());//查询条件 type 1,2 分别是菜品和套餐分类
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);//排序条件

        List<Category> list = categoryService.list(queryWrapper);//查询符合的对象

        return R.success(list);
    }
}
